package fr.pud.client.controller.address;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Address;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.PUDStationDatabase;
import fr.pud.client.model.SuggestionAddresses;
import fr.pud.client.view.AddressSuggestionsFrame;

/**
 * The action when the validate button is pressed in the frame add addresses.
 * Apr 8, 2012 - OSE
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         GetUserAddressChoices.java
 */
public class GetUserAddressChoices extends AbstractAction {
    /**
     * The serial uid if the frame should be serialized.
     */
    private static final long    serialVersionUID = -5063627875652291480L;
    /**
     * An arraylist of JCombobox, each JCombobox represents the different
     * results
     * for the address given as inputs.
     */
    private ArrayList<JComboBox> userChoices;
    /**
     * The frame which contains the list of combo box.
     */
    private JFrame               frame;

    /**
     * Default constructor.
     * It configures the button with name, description, icon ... and create the
     * references towards the ArrayList<JComboBox>
     * @param choices
     *            the ArrayList<JComboBox> to affect
     * @param suggestionsFrame
     *            the frame which contains
     */
    public GetUserAddressChoices(ArrayList<JComboBox> choices,
            AddressSuggestionsFrame suggestionsFrame) {
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.VALIDATE_ADDRESSES_CHOICE));
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.VALIDATE));
        this.putValue(SMALL_ICON, ImagesDatabase.VALIDATE);
        this.userChoices = choices;
        this.frame = suggestionsFrame;
    }

    /**
     * If an action is performed the controller checks if the inputs are correct
     * then it adds it to the database or warns the user that the inputs were
     * wrong.
     * At the end it dispose the frame.
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        for (JComboBox box : this.userChoices) {
            Object o = box.getSelectedItem();
            if (o instanceof Address) {
                PUDStation station = new PUDStation((Address) o);
                PUDStationDatabase.getInstance().getStations().add(station);
                SuggestionAddresses.getSuggestionAddresses().addSuggestionPUD(
                        station);
            }
        }
        this.frame.dispose();
        JOptionPane.showMessageDialog(null, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.SUCCESSFULL_OPERATION),
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.LOAD_ADDRESSES),
                JOptionPane.CLOSED_OPTION);
    }

    /**
     * @return the ArrayList<JComboBox> which represents the user choices
     */
    public ArrayList<JComboBox> getUserChoices() {
        return this.userChoices;
    }
}
